//
//  GRPhotoBrowserView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/8/4.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRPhotoBrowserView.h"
#define kMinZoomScale 0.6f
#define kMaxZoomScale 2.0f
@implementation GRPhotoBrowserView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.scrollerView];
    }
    return self;
}

-(UIScrollView *)scrollerView{
    if(!_scrollerView){
        _scrollerView = [[UIScrollView alloc]init];
        _scrollerView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        _scrollerView.showsVerticalScrollIndicator = NO;
        _scrollerView.showsHorizontalScrollIndicator = NO;

        [_scrollerView addSubview:self.imageView];
        _scrollerView.delegate = self;
        _scrollerView.clipsToBounds = YES;
    }
    return _scrollerView;
}

-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        _imageView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        _imageView.userInteractionEnabled = YES;
    }
    return _imageView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _scrollerView.frame = self.bounds;
    CGRect frame = self.frame;
    if (self.imageView.image) {
        CGSize imageSize = self.imageView.image.size;
        CGRect imageFrame = CGRectMake(0, 0, imageSize.width, imageSize.height);
        if (_isFullWidthForLandScape) {////图片宽度始终==屏幕宽度
            CGFloat ratio = frame.size.width/imageFrame.size.width;
            imageFrame.size.height = imageFrame.size.height*ratio;
            imageFrame.size.width = frame.size.width;
        }else{
            if (frame.size.width<=frame.size.height) {
                //竖屏时候
                CGFloat ratio = frame.size.width/imageFrame.size.width;
                imageFrame.size.height = imageFrame.size.height*ratio;
                imageFrame.size.width = frame.size.width;
            }else{ //横屏的时候
                CGFloat ratio = frame.size.height/imageFrame.size.height;
                imageFrame.size.width = imageFrame.size.width*ratio;
                imageFrame.size.height = frame.size.height;
            }
        }
        self.imageView.frame = imageFrame;
        self.scrollerView.contentSize = self.imageView.frame.size;
        self.imageView.center = [self centerOfScrollViewContent:self.scrollerView];
        CGFloat maxScale = frame.size.height/imageFrame.size.height;
        maxScale = frame.size.width/imageFrame.size.width >maxScale?frame.size.width/imageFrame.size.width:maxScale;
        maxScale = maxScale >kMaxZoomScale ?maxScale:kMaxZoomScale;
        self.scrollerView.minimumZoomScale = kMinZoomScale;
        self.scrollerView.maximumZoomScale = maxScale;
        self.scrollerView.zoomScale = 1.0f;
    }else{
        frame.origin = CGPointZero;
        self.imageView.frame = frame;
        self.scrollerView.contentSize = self.imageView.frame.size;
    }
     self.scrollerView.contentOffset = CGPointZero;
}

-(void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder{
    
    [_imageView sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [self setNeedsLayout];
    }];
}

#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView //这里是缩放进行时调整
{
    self.imageView.center = [self centerOfScrollViewContent:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.scrollOffset = scrollView.contentOffset;
}

- (CGPoint)centerOfScrollViewContent:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
    (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    CGPoint actualCenter = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
                                       scrollView.contentSize.height * 0.5 + offsetY);
    return actualCenter;
}
@end
